clear all

cd "~/Dropbox (Harvard University)/Monetary Policy with Heterogenous Producers/Replication Code"

ssc install grstyle, replace
grstyle init
grstyle set plain, nogrid
graph set window fontface "Palatino"


// CLEAN ALL QUARTERLY VARIABLES

// Import + clean log industrial production
import delimited "data/FRED/INDPRO.csv", clear
gen date_dt = date(date, "YMD")
gen year = yofd(date_dt)
gen quarter = quarter(date_dt)
collapse (mean) indpro, by(year quarter)
gen quarter_date = yq(year, quarter)
format quarter_date %tq
drop year quarter
rename quarter_date date
gen log_indpro = log(indpro)
tsset date
tsfilter hp log_indpro_hp=log_indpro
save "data/FRED/INDPRO_clean", replace
// Import + clean log CPI
import delimited "data/FRED/CPIAUCSL.csv", clear
gen date_dt = date(date, "YMD")
gen year = yofd(date_dt)
gen quarter = quarter(date_dt)
collapse (mean) cpiaucsl, by(year quarter)
gen quarter_date = yq(year, quarter)
format quarter_date %tq
drop year quarter
rename quarter_date date
gen log_cpi = log(cpiaucsl)
keep date log_cpi
tsset date
tsfilter hp log_cpi_hp=log_cpi
save "data/FRED/CPI_clean", replace
// Import + clean GZ_ExcessBond
import delimited "data/GZ_ExcessBond/ebp_csv.csv", clear
gen date_dt = date(date, "YMD")
gen year = yofd(date_dt)
gen quarter = quarter(date_dt)
collapse (mean) ebp, by(year quarter)
gen quarter_date = yq(year, quarter)
format quarter_date %tq
drop year quarter
rename quarter_date date
save "data/GZ_ExcessBond/ebp_clean", replace
// Import + clean Treasury Yield curve
import delimited "data/Treasury_Yield/feds200628.csv", clear rowrange(11:) varnames(10)
gen date_dt = date(date, "YMD")
gen year = yofd(date_dt)
gen quarter = quarter(date_dt)
gen T1Y = real(svenf01)
gen T2Y = real(svenf02)
collapse (mean) T1Y T2Y, by(year quarter)
gen quarter_date = yq(year, quarter)
format quarter_date %tq
drop year quarter
rename quarter_date date
save "data/Treasury_Yield/yield_clean", replace

// Import Gorodnichenko and Weber (2016) shocks
import excel "data/Gorodnichenko_Weber_2016/Gorodnichenko_Weber.xlsx", sheet("Sheet2") firstrow clear
drop if DATE==.
g quarter_start = dofq(qofd(DATE))
g quarter_end = dofq(qofd(DATE) + 1)
format quarter_start %td 
format quarter_end %td
g phi = (quarter_end - DATE) / (quarter_end - quarter_start)
g tight_surprise_phiwt = phi*tight_suprise 
g wide_surprise_phiwt = phi*wide_surprise 
g tight_surprise_antiphiwt = (1-phi)*tight_suprise
g wide_surprise_antiphiwt = (1-phi)*wide_surprise
collapse (sum) tight_surprise_phiwt wide_surprise_phiwt tight_surprise_antiphiwt wide_surprise_antiphiwt, by(quarter_start)
g date = qofd(quarter_start)
format date %tq
tsset date
g tight_surprise = tight_surprise_phiwt + L1.tight_surprise_antiphiwt
g wide_surprise = wide_surprise_phiwt + L1.wide_surprise_antiphiwt
replace tight_surprise = tight_surprise_phiwt if tight_surprise==.
replace wide_surprise = wide_surprise_phiwt if wide_surprise==.
keep date tight_surprise wide_surprise
save "data/Gorodnichenko_Weber_2016/GW_clean", replace

// Import Miranda-Agrippino and Ricco (2017) shocks
import excel "data/Miranda_Agrippino_Ricco_2017/ff4_instruments_shared.xlsx", firstrow clear
gen month = monthly(date, "YM")
format month %tm
drop date
gen date = dofm(month)
format date %td
g quarter_start = dofq(qofd(date))
g quarter_end = dofq(qofd(date) + 1)
format quarter_start %td 
format quarter_end %td
g phi = (quarter_end - date) / (quarter_end - quarter_start)
g MPI_FF4_phiwt = phi*MPI_FF4 
g INFO_FF4_phiwt = phi*INFO_FF4 
g MPI_FF4_antiphiwt = (1-phi)*MPI_FF4
g INFO_FF4_antiphiwt = (1-phi)*INFO_FF4
collapse (sum) MPI_FF4_phiwt INFO_FF4_phiwt MPI_FF4_antiphiwt INFO_FF4_antiphiwt, by(quarter_start)
g date = qofd(quarter_start)
format date %tq
tsset date
g MPI_FF4 = MPI_FF4_phiwt + L1.MPI_FF4_antiphiwt
g INFO_FF4 = INFO_FF4_phiwt + L1.INFO_FF4_antiphiwt
replace MPI_FF4 = MPI_FF4_phiwt if MPI_FF4==.
replace INFO_FF4 = INFO_FF4_phiwt if INFO_FF4==.
keep date MPI_FF4 INFO_FF4
save "data/Miranda_Agrippino_Ricco_2017/MAR_2017_clean", replace

import excel "data/Miranda_Agrippino_Ricco_2017/MPS_IV.xlsx", cellrange(A2) firstrow clear
rename Description date 
rename MirandaAgrippinoRicco2020 MPInew
gen month = monthly(date, "YM")
format month %tm
drop date
gen date = dofm(month)
format date %td
g quarter_start = dofq(qofd(date))
g quarter_end = dofq(qofd(date) + 1)
format quarter_start %td 
format quarter_end %td
g phi = (quarter_end - date) / (quarter_end - quarter_start)
g MPInew_phiwt = phi*MPInew 
g GertlerKaradi_phiwt = phi*GertlerKaradi
g FF4surprisesbyGSS_phiwt = phi*FF4surprisesbyGSS  
g MPInew_antiphiwt = (1-phi)*MPInew
g GertlerKaradi_antiphiwt = (1-phi)*GertlerKaradi
g FF4surprisesbyGSS_antiphiwt = (1-phi)*FF4surprisesbyGSS
collapse (sum) MPInew_*wt GertlerKaradi_*wt FF4surprisesbyGSS_*wt, by(quarter_start)
g date = qofd(quarter_start)
format date %tq
tsset date
g MPInew = MPInew_phiwt + L1.MPInew_antiphiwt
g GertlerKaradi = GertlerKaradi_phiwt + L1.GertlerKaradi_antiphiwt
g FF4surprisesbyGSS = FF4surprisesbyGSS_phiwt + L1.FF4surprisesbyGSS_antiphiwt
replace MPInew = MPInew_phiwt if MPInew==.
replace GertlerKaradi = GertlerKaradi_phiwt if GertlerKaradi==.
replace FF4surprisesbyGSS = FF4surprisesbyGSS_phiwt if FF4surprisesbyGSS==.
replace MPInew=. if MPInew==0
keep date MPInew GertlerKaradi FF4surprisesbyGSS
merge 1:1 date using "data/Miranda_Agrippino_Ricco_2017/MAR_2017_clean"
drop _merge
save "data/Miranda_Agrippino_Ricco_2017/MAR_2017_clean", replace

// Import Bauer and Swanson (2022) shocks
import excel "data/Bauer_Swanson_2022/FOMC_Bauer_Swanson.xlsx", sheet("final_excl_911") firstrow clear
drop if Date==.
g quarter_start = dofq(qofd(Date))
g quarter_end = dofq(qofd(Date) + 1)
format quarter_start %td 
format quarter_end %td
g phi = (quarter_end - Date) / (quarter_end - quarter_start)
g MPS_ORTH_phiwt = phi*MPS_ORTH 
g MPS_ORTH_antiphiwt = (1-phi)*MPS_ORTH
collapse (sum) MPS_ORTH_phiwt MPS_ORTH_antiphiwt, by(quarter_start)
g date = qofd(quarter_start)
format date %tq
tsset date
g MPS_ORTH = MPS_ORTH_phiwt + L1.MPS_ORTH_antiphiwt
replace MPS_ORTH = MPS_ORTH_phiwt if MPS_ORTH==.
keep date MPS_ORTH
save "data/Bauer_Swanson_2022/BS_clean", replace


////// START BUILDING FULL DATASET
// Import quarterly TFP data
import excel "data/markup_estimates/FRED/quarterly_tfp.xlsx", sheet("quarterly") firstrow clear
rename date quarter
keep quarter dY dhours dLQ dk dtfp dtfp_util alpha
drop if quarter==""
gen year = substr(quarter, 1, 4)
destring year, replace
save "data/markup_estimates/FRED/quarterly_tfp_clean", replace

gen date = quarterly(quarter, "YQ")
format date %tq
rename quarter quarter_str
g quarter = date

merge m:1 quarter using "data/markup_estimates/quarterly_mu_bar"
drop if _merge==2
drop _merge
merge m:1 year using "data/markup_estimates/aggregate_markup_mu_li_annual"
drop if _merge==2
drop _merge

gen labor_rev_share = 1 - alpha
gen labor_cost_share = mu_bar * labor_rev_share
gen dtfp_rep = dY - (1-alpha)*(dLQ+dhours) - alpha*dk
gen dtfp_labor = dY - (dLQ+dhours)
gen dtfp_distadj = dY - labor_cost_share*(dLQ+dhours) - (1-labor_cost_share)*dk

// Merge in Romer-Romer and MAR shocks
merge 1:1 date using "data/Monetary_shocks/RR_monetary_shock_quarterly"
drop _merge
merge 1:1 date using "data/Miranda_Agrippino_Ricco_2017/MAR_2017_clean"
drop _merge
merge 1:1 date using "data/Gorodnichenko_Weber_2016/GW_clean"
drop _merge
merge 1:1 date using "data/Bauer_Swanson_2022/BS_clean"
drop _merge

// Merge in all FRED series
merge 1:1 date using "data/FRED/INDPRO_clean"
drop if _merge==2
drop _merge
merge 1:1 date using "data/FRED/CPI_clean"
drop if _merge==2
drop _merge
merge 1:1 date using "data/GZ_ExcessBond/ebp_clean"
drop if _merge==2
drop _merge
merge 1:1 date using "data/Treasury_Yield/yield_clean"
drop if _merge==2
drop _merge

drop if year < 1973
drop if year > 2019
drop quarter  
tsset date 
save "data/data_temp/quarterly_alldata", replace
export delimited "data/data_temp/quarterly_alldata.csv", replace

/// MAIN ANALYSIS
* TODO: Add parallel measures for all the quarterly stuff: annual covariance, annual var?
use "data/data_temp/quarterly_alldata", clear
local var_list log_indpro_hp log_cpi_hp ebp T2Y
local var_list_endog log_indpro_hp log_cpi_hp ebp
var `var_list', lags(1/4) dfk small
foreach v in `var_list' {
	predict uvar_`v', residuals equation(`v')
}
gen u_structural = uvar_T2Y
foreach v in `var_list_endog' {
	ivregress 2sls uvar_`v' (u_structural = resid_full)
	estat firststage
	predict vvar_`v', residuals
}
ivregress 2sls uvar_T2Y (uvar_log_indpro_hp = vvar_log_indpro_hp)
ivregress 2sls uvar_T2Y (uvar_log_cpi_hp = vvar_log_cpi_hp)
ivregress 2sls uvar_T2Y (uvar_ebp = vvar_ebp)

corr(u_structural resid resid_romer resid_full MPI_FF4 GertlerKaradi FF4surprisesbyGSS MPS_ORTH)

qui regress u_structural resid_full
test resid_full
qui regress u_structural MPI_FF4
test MPI_FF4
qui regress u_structural GertlerKaradi
test GertlerKaradi
qui regress u_structural FF4surprisesbyGSS
test FF4surprisesbyGSS
qui regress u_structural tight_surprise
test tight_surprise
qui regress u_structural wide_surprise
test wide_surprise
qui regress u_structural MPS_ORTH
test MPS_ORTH

drop quarter_str 
rename date quarter
save "data/data_temp/quarterly_aggregates_clean", replace
